/*
 * @Author: JarvanIV 
 * @Date: 2018-10-08 13:33:16 
 * @Last Modified by: JarvanIV
 * @Last Modified time: 2018-11-01 13:50:26
 */
import { userLoginOut } from '@/hooks/imLogin'
import { noNet } from '@/utils/utils'
import { network } from "@/store/network";
export const BASE_URL = import.meta.env.VITE_API_HOST
export const PRO_BASE_URL = import.meta.env.VITE_API_HOST
//export const BASE_URL = "http://192.168.0.103:8080"
//export const BASE_URL = "http://lisongxixi.free.idcfengye.com"
// Axios.interceptors.request.use(function (config) {
// 	return config;
// }, function (error) {
// 	return Promise.reject(error);
// });

// Axios.interceptors.response.use(function (response) {
// 	return response;

// }, function (error) {
// 	return error;
// 	if (error.response === 404) {
// 		console.log(JSON.stringify(error));
// 	}
// });








const obj = {
	get(url:string , params:any,header={}) {
		return new Promise((resovle, reject) => {
			uni.request({
                url:`${BASE_URL}${url}`, //仅为示例，并非真实接口地址。
                header,
                data:params,
                success: (res) => {
                    const { statusCode } = res;
                    if(statusCode===200){
                        if(flag===9000||flag===9010||flag===9010||flag===9011||flag===9012){
                            uni.showToast({
                                icon: "none",
                                title: "身份验证过期,请重新登录",
                                duration: 5000,
                            });

                            // uni.showToast({
                            //     icon: "none",
                            //     title: "后台后台后台后台后台后台后台后台后台后台后台后台后台",
                            //     duration: 604800000,
                            //  });

                            try {
                                uni.$TUIKit.logout().then((res) => {});
                            } catch (error) {
                                
                            }

                            try {
                                userLoginOut();
                            } catch (error) {
                                
                            }
                        }

                        resovle(res.data);
                    }else{
                        reject(`statusCode:${statusCode}`)
                    }
                    
                    //获得token完成登录
                    //uni.setStorageSync('token',res.token)
                },
                fail(err) {
                    reject(err);
                },

            });
		})
	},
	post(url:string , data:any,header={}) {
		//process.client&&Indicator.open();
		return new Promise((resovle, reject) => {
			uni.request({
                url:`${BASE_URL}${url}`, //仅为示例，并非真实接口地址。
                header,
                method:'POST',
                data,
                success: (res) => {
                    const NETWORK = network();
                    NETWORK.setNet(true);

                    // uni.showToast({
                    //     icon: "none",
                    //     title: import.meta.env.VITE_APP_PUSH_ID,
                    //     duration: 999999,
                    // });

                    //import.meta.env.VITE_APP_PUSH_ID

                    const { statusCode } = res;
                    if(statusCode===200){
                        const {flag} = res.data;
                        if(flag===9000||flag===9010||flag===9010||flag===9011||flag===9012){
                            console.log('eeeeeeeeeee')
                            console.log('eeeeeeeeeee')
                            console.log('eeeeeeeeeee')
                            console.log(url)
                            // uni.showToast({
                            //     icon: "none",
                            //     title: "身份验证过期,请重新登录",
                            //     duration: 1500,
                            // });

                            try {
                                uni.$TUIKit.logout().then((res) => {});
                            } catch (error) {
                                
                            }

                            try {
                                userLoginOut();
                            } catch (error) {
                                
                            }
                        }else{
                            resovle(res.data);
                        }
                        
                    }else{
                        reject(`statusCode:${statusCode}`)
                    }
                    //获得token完成登录
                    //uni.setStorageSync('token',res.token)
                },
                fail(err) {
                    uni.getNetworkType({
                        async success(res) {

                            const token = uni.getStorageSync('api_token');
                            if (res.networkType === "none"&&token) {

                                noNet();

                                const NETWORK = network();
                                NETWORK.setNet(false);
                                
                                // uni.showToast({
                                //     icon: "none",
                                //     title: "没有网络,请检查",
                                //     duration: 2500,
                                // });
                                return;
                            } 
                        },
                    });

                    reject(err);
                },
            });
		})
	},

}



//export const AxiosCase=Axios
export default obj